package com.cloud.mall.pms.service.impl;

import com.cloud.mall.api.CommonPage;
import com.cloud.mall.base.BaseResponse;
import com.cloud.mall.core.bean.MallBeanUtils;
import com.cloud.mall.pms.dao.PmsProductCategoryAttributeRelationDao;
import com.cloud.mall.pms.dao.PmsProductCategoryDao;
import com.cloud.mall.pms.dao.PmsSkuStockDao;
import com.cloud.mall.pms.dto.PmsProductCategoryParam;
import com.cloud.mall.pms.dto.PmsProductCategoryWithChildrenItem;
import com.cloud.mall.pms.input.PmsSkuStockInpDTO;
import com.cloud.mall.pms.mapper.PmsProductCategoryAttributeRelationMapper;
import com.cloud.mall.pms.mapper.PmsProductCategoryMapper;
import com.cloud.mall.pms.mapper.PmsProductMapper;
import com.cloud.mall.pms.mapper.PmsSkuStockMapper;
import com.cloud.mall.pms.model.*;
import com.cloud.mall.pms.out.PmsProductCategoryOut;
import com.cloud.mall.pms.out.PmsSkuStockOut;
import com.cloud.mall.pms.service.PmsProductCategoryService;
import com.cloud.mall.pms.service.PmsSkuStockService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class PmsSkuStockServiceImpl implements PmsSkuStockService {
    @Autowired
    private PmsSkuStockMapper skuStockMapper;
    @Autowired
    private PmsSkuStockDao skuStockDao;

    @Override
    public BaseResponse update(Long pid, List<PmsSkuStockInpDTO> skuStockList) {
        List<PmsSkuStock> pmsSkuStocks=MallBeanUtils.copyDoToDto(skuStockList,PmsSkuStock.class);
        int count =skuStockDao.replaceList(pmsSkuStocks);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }

    @Override
    public BaseResponse<List<PmsSkuStockOut>> getList(Long pid, String keyword) {
        PmsSkuStockExample example = new PmsSkuStockExample();
        PmsSkuStockExample.Criteria criteria = example.createCriteria().andProductIdEqualTo(pid);
        if (!StringUtils.isEmpty(keyword)) {
            criteria.andSkuCodeLike("%" + keyword + "%");
        }
        List<PmsSkuStock> skuStockList =skuStockMapper.selectByExample(example);
        return BaseResponse.setResultSuccess(MallBeanUtils.copyDoToDto(skuStockList,PmsSkuStockOut.class));
    }
}
